
/**
 * The Application's header
 */
qx.Class.define("HersPOS.ItemBar",
{
  extend : qx.ui.toolbar.ToolBar,

  events :
  {
    "done"          : "qx.event.type.Event",
    "reload"        : "qx.event.type.Event",
    "reload_selbox" : "qx.event.type.Event"
  },

  construct : function()
  {
    this.base(arguments);

    //     var layout= qx.ui.layout.VBox()
    //     this.setLayout(layout)
    //     this.InsertBar = new qx.ui.toolbar.ToolBar();
    //     this.InsertBar.setDecorator(null)
    var common_text_setting =
    {
      alignY     : "middle",
      alignX     : "center",
      allowGrowY : false
    };

    this.add_btn = new qx.ui.toolbar.Button("Add", "HersPOS/qx/icon/Oxygen/16/actions/list-add.png").set({ alignY : "middle" });

    var items_add_part = new qx.ui.toolbar.Part();
    var food_lbl = new qx.ui.basic.Label("Food Name").set({ alignY : "middle" });
    var food_name_txt = new qx.ui.form.TextField().set(common_text_setting);
    var food_price_lbl = new qx.ui.basic.Label("Price :").set({ alignY : "middle" });
    var food_price_txt = new qx.ui.form.TextField().set(common_text_setting);
    var base_price_lbl = new qx.ui.basic.Label("Base Price:").set({ alignY : "middle" });
    var base_price_txt = new qx.ui.form.TextField().set(common_text_setting);

    //  edit_tb_part.addSeparator()
    items_add_part.add(food_lbl);
    items_add_part.add(food_name_txt);
    items_add_part.add(food_price_lbl);
    items_add_part.add(food_price_txt);
    items_add_part.add(base_price_lbl);
    items_add_part.add(base_price_txt);
    items_add_part.addSeparator();
    items_add_part.add(this.add_btn);

    this.add_btn.addListener("execute", function(e)
    {
      var food_name = food_name_txt.getValue();
      var food_price = food_price_txt.getValue();
      var food_base = base_price_txt.getValue();
      this.post_items(food_name, food_price, food_base);
    },
    this);

    this.post_items = function(food_name, price, base_price)
    {
      var req = new qx.io.request.Xhr("/HersPOS/default/insert_items.json?food_name=" + food_name + "&price=" + price + "&base_price=" + base_price);

      req.addListener("success", function(e)
      {
        var req = e.getTarget();
        this.fireEvent("done");

        // Response parsed according to the server's
        // response content type, e.g. JSON
        req.getResponse();
      },
      this);

      // Send request
      req.send();
    };

    this.addListener("done", function() {
      this.fireEvent("reload");

    }, this);

    //     this.addSpacer()
    this.add(items_add_part);
  }
});